$(document).ready(function () {

    layui.use(['form', 'laydate', 'upload', 'laypage', 'layer', 'table', 'element'], function () {
        var $ = layui.jquery, //调用layui内置jquery,
            form = layui.form, //表单,
            element = layui.element, //Tab的切换功能，切换事件监听等，需要依赖element模块,
            laydate = layui.laydate, //日期,
            laypage = layui.laypage, //分页,
            upload = layui.upload, //上传,
            table = layui.table, //表格,
            layer = layui.layer; //弹层

        //初始化 layui 插件

        //验证规则
        // form.verify({
        //     required: function (value, item) { //value：表单的值、item：表单的DOM对象
        //         var name = $(item).attr('placeholder');
        //         name = (name != null && name != '') ? name : $(item).attr('name');
        //         if (value == '') {
        //             return 'Please Enter ' + name;
        //         }
        //     }
        // });

        //跳过图片上传复选框
        form.on('checkbox(uploadLater)', function (obj) {
            if (vm.orderItem.fileList.length > 0) {
                form.val("upload", {
                    "isUploadLater": false
                });
                vm.orderItem.isUploadLater = false;
            }
            else {
                vm.orderItem.isUploadLater = obj.elem.checked;
            }
        });

        //确定是否使用优惠券复选框
        form.on('checkbox(isUseCoupon)', function (obj) {
            vm.useCouponDiscount(obj.elem.checked);
            form.val("orderForm", {
                "isUseCoupon": vm.orderItem.isUseCoupon
            });
        });

        form.on('radio(size)', function (obj) {
            vm.orderItem.selectedSize = obj.value;
        });

        form.on('radio(quantity)', function (obj) {
            vm.orderItem.selectedQuantity = obj.value;
            if (vm.orderItem.size <= 0) {
                $.zero.showErrorMsg('You must first select Size<br>in order to set a custom quantity');
                return;
            }
            if (vm.orderItem.embroideryCoverageRequired === true && vm.orderItem.embroideryCoverageId <= 0) {
                $.zero.showErrorMsg('You must first select Embroidery Coverage<br>in order to set a custom quantity');
                return;
            }
        });

        var _canSelectProduct = true;
        var _canSubmitOrder = true;

        var specialOptionItemTemp = {
            cateId: 0,
            cateName: '',
            itemId: 0,
            itemName: ''
        };

        var orderItemTemp = {
            quoteId: 0,
            t: '',
            name: '',
            email: '',
            phone: '',
            productCategoryId: 0,
            productCategory: '',
            productId: 0,
            product: '',
            productPhoto: '',
            productPhotoShow: '',
            fileList: [],
            fileData: '',
            isUploadLater: false,
            youMessage: '',
            width: '',
            height: '',
            size: 0,
            minSize: 0.5,
            maxSize: 14,
            selectedSize: '',
            embroideryCoverageId: 0,
            embroideryCoverage: '',
            embroideryCoverageRequired: false,
            typeId: 0,
            type: '',
            typeRequired: false,
            minQuantity: 1,
            maxQuantity: 50000,
            quantity: 0,
            unitPrice: 0,//单价
            discount: 0,//折扣比例
            discountPrice: 0,//折扣单价
            originalAmount: 0,//原金额
            discountAmount: 0,//折扣金额
            isUseCoupon: false,//是否使用优惠卷
            couponCode: '', //优惠代码
            couponDiscount: 0,//优惠折扣比例
            couponAmoun: 0,//优惠金额
            customQuantity: '',
            selectedQuantity: '',
            specialOptionIds: '',
            specialOptionList: [
                // {cateId:0,cateName:'Backing',itemId:0,itemName:'No Backing',photo:'',rate:'10',extraAmount:'5'}
            ],
            specialOptionData: '',
            specialOptionFee: 0,
            turnaroundTypeId: '',
            turnaroundType: '',
            turnaroundDate: '',
            turnaroundDateRange: '',
            country: '',
            apartment: '',
            zipCode: '',
            city: '',
            stateAndProvince: '',
            address: '',
            subTotal: 0,//小计
            expediteFee: 0,//周转费用
            additional: 0,//折扣
            finalTotal: 0,//最终价格
        };

        var _priceBase = [];
        var _priceDiscount = [];
        var _priceConfig = [];
        var _productConfig = [];
        var _quantityConfig = [];

        var vm = new Vue({
            el: '#container',
            data: {
                quoteId: '',
                t: '',
                stepProcess: [{ id: 1, name: 'Product', continueStatus: false },
                { id: 2, name: 'Customize', continueStatus: false },
                { id: 3, name: 'Special Options', continueStatus: true },
                { id: 4, name: 'Upload Artwork', continueStatus: false },
                { id: 5, name: 'Turnaround', continueStatus: true }],
                selectedProcessIndex: 3,
                selectedProductId: -1,
                tradeGategroyIds: '66,4,2,101',
                //tradeProductIds:'4,2,101',
                productNode: {},
                sizeNode: null,
                quantityNode: null,
                embroideryCoverageNode: null,
                typeNode: null,
                specialOptionNode: null,
                turnaroundTypeNode: {},
                order: {},
                quote: null,
                orderItemCacheList: [],
                orderItem: $.extend(true, {}, orderItemTemp)
            },
            created: function () {
                this.orderItem.quoteId = $.zero.getQueryString('quoteid', '');
                this.orderItem.t = $.zero.getQueryString('t', '');
                if (this.orderItem.quoteId === '' || this.orderItem.t === '') {
                    this.orderItem.quoteId = $.zero.getCookieWithKey('quoteId');
                    this.orderItem.t = $.zero.getCookieWithKey('t');
                }

                //创建前，加载需要的数据
                layer.load(2);
                var me = this;

                $.when(
                    $.ajax({
                        type: "get",
                        url: "/api/tradeConfigOrderTree.json"
                    }),
                    $.ajax({
                        type: "get",
                        data: { quoteId: this.orderItem.quoteId, t: this.orderItem.t },
                        url: "/api/getQuote.json"
                    })
                ).done(function (data1, data3) {
                    layer.closeAll('loading');
                    vm.initPage(data1[0][1], data3[0].result, data1[0][0]);
                }).fail(function () {
                    layer.closeAll('loading');
                    // $.zero.showErrorMsg('Sorry Server Lost');
                });
            },
            watch: {
                'selectedProcessIndex': function (newValue, oldValue) {
                    $.zero.setCurrentQueryString('p', newValue);
                },
                'orderItem.fileList': function (newValue, oldValue) {
                    if (this.orderItem.fileList.length > 0) {
                        this.orderItem.isUploadLater = false;
                    }
                    this.getProductPhoto();
                    this.setProcessContinueStatus(4);
                },
                'orderItem.isUploadLater': function (newValue, oldValue) {
                    form.val("upload", {
                        "isUploadLater": this.orderItem.isUploadLater
                    });
                    this.setProcessContinueStatus(4);
                },
                'orderItem.width': function (newValue, oldValue) {
                    vm.changeWidth();
                },
                'orderItem.height': function (newValue, oldValue) {
                    vm.changeHeight();
                },
                'orderItem.selectedSize': function (newValue, oldValue) {
                    this.changeSize();
                },
                'orderItem.typeId': function (newValue, oldValue) {
                    if (this.quote == null || this.quote.typeId != this.orderItem.typeId) {
                        $.zero.setCurrentQueryString('tt', this.orderItem.typeId);
                    }
                    this.setProcessContinueStatus(2);
                    //循环判断选中的是否有不可用的,移除已选中的
                    //this.computePrice();
                },
                'orderItem.embroideryCoverageId': function (newValue, oldValue) {
                    if (this.quote == null || this.quote.embroideryCoverageId != this.orderItem.embroideryCoverageId) {
                        $.zero.setCurrentQueryString('e', this.orderItem.embroideryCoverageId);
                    }
                    this.setProcessContinueStatus(2);
                    this.computePrice();
                },
                'orderItem.customQuantity': function (newValue, oldValue) {
                    vm.changeCustomQuantity();
                },
                'orderItem.selectedQuantity': function (newValue, oldValue) {
                    this.changeQuantity();
                },
                'orderItem.turnaroundDate': function (newValue, oldValue) {
                    if (this.quote.turnaroundDate != '' && this.quote.turnaroundDate != this.orderItem.turnaroundDate) {
                        $.zero.setCurrentQueryString('d', this.orderItem.turnaroundDate);
                    }

                    if (this.orderItem.turnaroundDate.length == 0) {
                        $.zero.removeCurrentQueryString('d');
                    }
                },
                'orderItem.couponCode': function (newValue, oldValue) {
                    if (newValue !== oldValue && this.orderItem.isUseCoupon) {
                        form.val("orderForm", {
                            "isUseCoupon": false
                        });
                        this.useCouponDiscount(false);
                    }
                }
            },
            methods: {
                fromRender: function () {
                    //重新渲染layui
                    console.log('form.render');
                    vm = this;
                    this.$nextTick(function () {
                        if (this.orderItem.size > 0) {
                            var size = $.zero.find(this.sizeNode.children, { 'name': this.orderItem.size.toString() });

                            if (size != null) {
                                this.orderItem.selectedSize = this.orderItem.size.toString();
                                form.val("customize", {
                                    "size": this.orderItem.selectedSize
                                });
                            }
                        }
                        else {
                            $("input[name='size']").each(function (index, el) { el.checked = false; });
                        }

                        if (this.orderItem.quantity > 0) {
                            var quantity = $.zero.find(this.quantityNode.children, { 'name': this.orderItem.quantity.toString() });

                            if (quantity != null) {
                                this.orderItem.selectedQuantity = this.orderItem.quantity.toString();
                                form.val("customize", {
                                    "quantity": this.orderItem.selectedQuantity
                                });
                            }
                            else {
                                this.orderItem.customQuantity = this.orderItem.quantity.toString();
                            }
                        }
                        else {
                            $("input[name='quantity']").each(function (index, el) { el.checked = false; });
                        }

                        form.render();
                        /*api地址http://www.layui.com/doc/modules/upload.html */
                        var uploadInstFaceFile = upload.render({
                            elem: '#uploadFile', //绑定元素
                            accept: 'file', //允许上传的文件类型
                            //acceptMime: 'image/jpeg, image/jpg, image/png',//规定打开文件选择框时，筛选出的文件类型。
                            multiple: false, //允许多文件上传
                            exts: 'jpg|png|gif|bmp|jpeg|pdf|psd|tif|tiff|pic|pct|pga|eps|cdr|ai|hpgl|dxf|autocad',//允许上传的文件后缀
                            //auto: false, //选完文件后不要自动上传
                            //bindAction: '#xunjiaSubmit', //指定一个按钮触发上传
                            url: '/api/upload', //上传接口
                            number: 1,//设置同时可上传的文件数量
                            size: 10000, //最大允许上传的文件大小
                            progress: function (e, percent) {
                                element.progress('progressBar', percent + '%');
                            },
                            choose: function (obj) {

                            },
                            before: function (obj) {
                                $('#uploadProgress').removeClass('hide');
                            },
                            done: function (res, index, upload) {
                                if (res.success) {
                                    vm.orderItem.fileList.push(res.result);
                                }
                                else {
                                    $.zero.showErrorMsg(res.error.message);
                                }

                                $('#uploadProgress').addClass('hide');
                                element.progress('progressBar', '0%');
                            },
                            error: function () {
                                $.zero.showErrorMsg(res.error.message);

                                $('#uploadProgress').addClass('hide');
                                element.progress('progressBar', '0%');
                            }
                        });   //quote文件上传

                        //物流
                        if (this.turnaroundTypeNode != null && this.turnaroundTypeNode.children != null) {
                            for (var i = 0; i < this.turnaroundTypeNode.children.length; i++) {
                                var turnaroundType = this.turnaroundTypeNode.children[i];
                                var currentDate = new Date();

                                //var minDay = turnaroundType.title == null ? '' : parseInt(turnaroundType.title);
                                //var maxDay = turnaroundType.subTitle == null ? '' : parseInt(turnaroundType.subTitle);
                                var minDateString = $.zero.addDate(currentDate, parseInt(turnaroundType.title));
                                var maxDateString = $.zero.addDate(currentDate, parseInt(turnaroundType.subTitle));

                                if (this.orderItem.turnaroundDate.length > 0 && this.orderItem.turnaroundTypeId == turnaroundType.id) {
                                    if ($.zero.compareDate(minDateString, this.orderItem.turnaroundDate)) {
                                        this.orderItem.turnaroundDate = '';
                                    }
                                    //if ($.zero.compareDate(this.orderItem.turnaroundDate, maxDateString)) {
                                    //    this.orderItem.turnaroundDate = '';
                                    //}
                                }
                                var value = this.orderItem.turnaroundTypeId == turnaroundType.id ? this.orderItem.turnaroundDate : '';

                                laydate.render({
                                    elem: '#datetime' + turnaroundType.id,
                                    no: i + 1,
                                    lang: 'en',
                                    position: 'fixed',
                                    min: minDateString,
                                    //max: maxDateString,
                                    value: value,
                                    isInit: true,
                                    showBottom: false,
                                    zIndex: 99999999,
                                    done: function (value, date, endDate) {
                                        vm.orderItem.turnaroundDate = value;
                                    },
                                    ready: function (data) {
                                        var startDate = new Date(data.year + '-' + data.month + '-' + data.date);
                                        minDateString = $(this.elem).attr('data-minDate');
                                        var endDate = new Date(minDateString);
                                        var month = $.zero.getIntervalMonth(startDate, endDate);
                                        if (month > 0) {
                                            for (var j = 0; j < month; j++) {
                                                var id = '#layui-laydate' + this.no + ' .laydate-next-m';
                                                $(id).click();
                                            }
                                        }
                                    }
                                }); //初始化日期控件
                                $('#datetime' + turnaroundType.id).attr('data-minDate', minDateString);
                                $('#datetime' + turnaroundType.id).val(value);
                            }
                        }

                        $('input[name="width"]').off("focus").focus(function () {
                            var newValue = $('input[name="width"]').val();
                            vm.changeWidth(newValue);
                        });//自定义宽度

                        $('input[name="height"]').off("focus").focus(function () {
                            var newValue = $('input[name="height"]').val();
                            vm.changeHeight(newValue);
                        });//自定义高度

                        $('input[name="customQuantity"]').off("focus").focus(function () {
                            var newValue = $('input[name="customQuantity"]').val();
                            vm.changeCustomQuantity(newValue);
                        });//自定义数量

                        form.on('submit(submitOrder)', function (data) {
                            vm.submitOrder(data);
                        });
                    });
                },
                changeWidth: function () {
                    if (vm.orderItem.selectedSize !== 'Custom Size') {
                        vm.orderItem.selectedSize = 'Custom Size';
                        form.val("customize", {
                            "size": vm.orderItem.selectedSize
                        });
                    }
                    vm.changeSize();
                },
                changeHeight: function () {
                    if (vm.orderItem.selectedSize !== 'Custom Size') {
                        vm.orderItem.selectedSize = 'Custom Size';
                        form.val("customize", {
                            "size": vm.orderItem.selectedSize
                        });
                    }
                    vm.changeSize();
                },
                changeCustomQuantity: function (newValue) {
                    if (vm.orderItem.selectedQuantity !== 'Custom Quantity') {
                        vm.orderItem.selectedQuantity = 'Custom Quantity';
                        form.val("customize", {
                            "quantity": vm.orderItem.selectedQuantity
                        });
                    }
                    vm.changeQuantity();
                },
                initPage: function (data1, data3, data4) {
                    $('#orderSide').removeClass('hide');

                    _productConfig = data1;
                    _quantityConfig = JSON.parse(data4.children[0].configData);

                    //分类支持产品过滤
                    if (this.tradeProductIds != null) {
                        var tradeProductIds = this.tradeProductIds.split(',');
                        this.productNode = $.zero.where(_productConfig.children, function () {
                            return tradeProductIds.indexOf(this.id.toString()) != -1;
                        });
                    }
                    else {
                        this.productNode = _productConfig.children;
                    }

                    this.orderItem.productCategoryId = $.zero.getQueryString('categoryId', '');
                    this.orderItem.productCategory = $.zero.getQueryString('category', '');
                    var productId = $.zero.getQueryString('productId', '');

                    this.quote = data3;
                    var productItem = null;
                    if (this.quote != null) { //表单生成
                        $.extend(true, this.orderItem, this.quote);
                        productItem = $.zero.find(this.productNode, 'id', this.orderItem.productId);
                        this.loadProduct(productItem, false);
                    }
                    else if (productId != '') {//产品分类生成
                        productItem = $.zero.find(this.productNode, 'id', productId);
                        this.selectedProduct(productItem);
                    }
                    else {
                        this.selectedProcessIndex = 1;
                    }
                },
                submitAddQuote: function (productItem) {
                    //todo:name，以后台为主,不用传递；缺少产品分类Id，可通过主页面传递
                    if (_canSelectProduct === false) return;
                    _canSelectProduct = false;

                    var data = {
                        productId: productItem.id,
                        product: productItem.name,
                        productCategoryId: vm.orderItem.productCategoryId,
                        productCategory: vm.orderItem.productCategory
                    };

                    $('#selectProduct' + productItem.id).html('Loading product...');
                    $.ajax({
                        type: "post",
                        url: "/api/addAutoQuote",
                        data: data,
                        success: function (msg) {
                            if (msg.success) {
                                vm.orderItem = $.extend(true, {}, orderItemTemp);
                                $.zero.clearCurrentQueryString();
                                vm.orderItem.quoteId = msg.result.id;
                                vm.orderItem.t = msg.result.token;
                                $.zero.setCurrentQueryString('quoteid', vm.orderItem.quoteId);
                                $.zero.setCurrentQueryString('t', vm.orderItem.t);
                                vm.orderItem.productId = productItem.id;
                                vm.orderItem.product = productItem.name;
                                vm.orderItem.productCategoryId = data.productCategoryId;
                                vm.orderItem.productCategory = data.productCategory;
                                vm.loadProduct(productItem, true);
                            }
                            else {
                                $.zero.showValidationErrorMsg(msg);
                            }
                            _canSelectProduct = true;
                            $('#selectProduct' + productItem.id).html('Select');
                        },
                        error: function () {
                            // $.zero.showErrorMsg('Sorry Server Lost');
                            _canSelectProduct = true;
                            $('#selectProduct' + productItem.id).html('Select');
                        }
                    });
                },
                submitUpdateQuote: function (index) {
                    this.orderItem.fileData = JSON.stringify(this.orderItem.fileList);
                    $.ajax({
                        type: "post",
                        url: "/api/updateAutoQuote",
                        data: this.orderItem,
                        success: function (msg) {
                            if (msg.success) {
                                var keys = 's,q,so,w,h,e,tt,ttt,d';
                                $.zero.removeCurrentQueryString(keys);
                            }
                            else {
                                $.zero.showValidationErrorMsg(msg);
                            }
                        },
                        error: function () {
                            // $.zero.showErrorMsg('Sorry Server Lost');
                        }
                    });
                },
                getProductPhoto: function () {
                    for (var i = 0; i < this.orderItem.fileList.length; i++) {
                        if ($.zero.contain(this.orderItem.fileList[i].type, 'image')) {
                            var domainUrl = this.orderItem.fileList[i].site === 'mobile' ? sizeConfig.mobileSite : sizeConfig.pcSite;
                            this.orderItem.productPhotoShow = domainUrl + this.orderItem.fileList[i].url;
                            break;
                        }
                    }

                    if (this.orderItem.productPhotoShow === '' || this.orderItem.fileList.length === 0) {
                        this.orderItem.productPhotoShow = this.orderItem.productPhoto;
                    }
                },
                selectedProduct: function (productItem) {
                    if (_canSelectProduct == false) return false;
                    if (this.orderItem.productId !== productItem.id) {
                        //获取刚才生成的
                        this.submitAddQuote(productItem);
                    }
                    else {
                        this.loadProduct(productItem, true);
                    }
                },
                loadProduct: function (productItem, isContinue) {
                    if (productItem === null) return false;
                    this.orderItem.productPhoto = this.getPhoto(productItem.photoData, 0, 200, 0);
                    this.getProductPhoto();
                    //规格
                    var customize = $.zero.find(productItem.children, { 'name': 'Customize' });
                    if (customize != null) {
                        this.sizeNode = $.zero.find(customize.children, { 'name': 'Size' });
                        this.quantityNode = $.zero.find(customize.children, { 'name': 'Quantity' });
                        this.embroideryCoverageNode = $.zero.find(customize.children, { 'name': 'Embroidery Coverage' });
                        this.typeNode = $.zero.find(customize.children, { 'name': 'Types' });
                        if (this.embroideryCoverageNode != null && this.embroideryCoverageNode.children != null) {
                            this.orderItem.embroideryCoverageRequired = true;
                        }
                        if (this.typeNode != null && this.typeNode.children != null) {
                            this.orderItem.typeRequired = true;
                        }
                        this.orderItem.minSize = this.sizeNode.title !== null ? parseFloat(this.sizeNode.title) : 0.5;
                        this.orderItem.maxSize = this.sizeNode.subTitle !== null ? parseFloat(this.sizeNode.subTitle) : 14;
                        this.orderItem.minQuantity = this.quantityNode.title !== null ? parseInt(this.quantityNode.title) : 1;
                        this.orderItem.maxQuantity = this.quantityNode.subTitle !== null ? parseInt(this.quantityNode.subTitle) : 50000;
                    }
                    else {
                        this.sizeNode = null;
                        this.quantityNode = null;
                        this.embroideryCoverageNode = null;
                        this.typeNode = null;
                    }

                    //特殊选项
                    this.specialOptionNode = $.zero.find(productItem.children, { 'name': 'Special Options' });

                    //周转
                    var turnaround = $.zero.find(productItem.children, { 'name': 'Turnaround' });
                    if (turnaround != null) {
                        this.turnaroundTypeNode = $.zero.find(turnaround.children, { 'name': 'Turnaround Type' });
                    }
                    else {
                        this.turnaroundTypeNode = null;
                    }

                    //加载价格配置
                    if (productItem.priceBase !== undefined || productItem.priceDiscount !== undefined) {
                        vm.renderProduct(isContinue);
                        layer.closeAll('loading');
                    }
                    else {
                        //价格配置
                        var typeBaseIds = [];
                        var typeDiscountIds = [];
                        if (this.embroideryCoverageNode != null && this.embroideryCoverageNode.children != null) {
                            for (var i = 0; i < this.embroideryCoverageNode.children.length; i++) {
                                typeBaseIds.push('b-' + this.embroideryCoverageNode.children[i].id);
                                typeDiscountIds.push('d-' + this.embroideryCoverageNode.children[i].id);
                            }
                        }
                        else {
                            typeBaseIds.push('b-' + productItem.id);
                            typeDiscountIds.push('d-' + productItem.id);
                        }

                        layer.load(2);
                        $.ajax({
                            type: "post",
                            url: "/api/priceList.json",
                            data: { productId: productItem.id, typeBaseIds: typeBaseIds, typeDiscountIds: typeDiscountIds },
                            success: function (msg) {
                                productItem.priceBase = $.zero.where(msg, function () {
                                    return typeBaseIds.indexOf(this.type) != -1;
                                });

                                productItem.priceDiscount = $.zero.where(msg, function () {
                                    return typeDiscountIds.indexOf(this.type) != -1;
                                });

                                vm.renderProduct(isContinue);

                                layer.closeAll('loading');
                            },
                            error: function () {
                                layer.closeAll('loading');
                                // $.zero.showErrorMsg('Sorry Server Lost');
                            }
                        });
                    }
                },
                renderProduct: function (isContinue) {
                    //todo：调整顺序
                    this.initProcessContinueStatus();
                    this.initOrderItem();
                    this.fromRender();

                    //this.getPrice(typeBaseIds, typeDiscountIds);

                    isContinue === true ? this.continueProcess(2) : this.initProcess();
                },
                initOrderItem: function () {
                    var size = parseFloat($.zero.getQueryString('s', '0'));
                    var width = parseFloat($.zero.getQueryString('w', '0'));
                    var height = parseFloat($.zero.getQueryString('h', '0'));

                    if (width > 0 && height > 0) {
                        this.orderItem.width = width.toString();
                        this.orderItem.height = height.toString();
                    }
                    else if (size > 0) {
                        this.orderItem.size = size;
                    }

                    var embroideryCoverageId = parseInt($.zero.getQueryString('e', '0'));
                    if (embroideryCoverageId > 0) {
                        this.orderItem.embroideryCoverageId = embroideryCoverageId;
                    }

                    var typeId = parseInt($.zero.getQueryString('tt', '0'));
                    if (typeId > 0) {
                        this.orderItem.typeId = typeId;
                    }

                    var quantity = parseInt($.zero.getQueryString('q', '0'));

                    if (quantity > 0) {
                        this.orderItem.quantity = quantity;
                    }


                    var specialOptionIds = $.zero.getQueryString('so', '');
                    if (specialOptionIds != '') {
                        this.orderItem.specialOptionIds = specialOptionIds;
                    }

                    if (this.orderItem.specialOptionIds != null && this.orderItem.specialOptionIds != '' && this.specialOptionNode != null && this.specialOptionNode.children != null) {
                        var specialOptionIdList = this.orderItem.specialOptionIds.split(',');

                        for (var i = 0; i < this.specialOptionNode.children.length; i++) {
                            var specialOptionCategroy = this.specialOptionNode.children[i];

                            if (specialOptionCategroy.children != null) {
                                for (var j = 0; j < specialOptionCategroy.children.length; j++) {
                                    var specialOptionItem = specialOptionCategroy.children[j];
                                    if (specialOptionIdList.indexOf(specialOptionItem.id.toString()) != -1) {
                                        this.changeSpecialOptionSelectedStatus(specialOptionCategroy, specialOptionItem, true);
                                    }
                                }
                            }
                        }
                    }

                    var turnaroundTypeId = parseInt($.zero.getQueryString('ttt', '0'));
                    if (turnaroundTypeId > 0) {
                        this.orderItem.turnaroundTypeId = turnaroundTypeId;
                    }

                    if (this.orderItem.turnaroundTypeId > 0 && this.turnaroundTypeNode != null && this.turnaroundTypeNode.children != null) {
                        var turnaroundType = $.zero.find(this.turnaroundTypeNode.children, { 'id': this.orderItem.turnaroundTypeId });
                        if (turnaroundType != null) {
                            this.changeTurnaroundTypeSelectedStatus(turnaroundType, true);
                        }
                    }

                    var turnaroundDate = $.zero.getQueryString('d', '');
                    if (turnaroundDate != '') {
                        this.orderItem.turnaroundDate = turnaroundDate;
                    }

                    this.computeFee();
                },
                initProcess: function () {
                    var selectedProcessIndex = $.zero.getQueryString('p', null);
                    if (selectedProcessIndex === null) {
                        selectedProcessIndex = 3;
                    }
                    else {
                        selectedProcessIndex = parseInt(selectedProcessIndex);

                        if (this.orderItem.fileList.length <= 0) {
                            this.orderItem.isUploadLater = true;
                        }
                    }

                    this.loadProcess(selectedProcessIndex);
                },
                initProcessContinueStatus: function () {
                    //初始的时候可以循环一遍，就可以判断该状态，然后跳跃
                    for (var i = 1; i <= this.stepProcess.length; i++) {
                        this.setProcessContinueStatus(i);
                    }
                },
                setProcessContinueStatus: function (index) {
                    this.stepProcess[index - 1].continueStatus = this.checkProcessContinueStatus(index).status;
                },
                continueProcess: function (index) {
                    var result = this.checkProcessContinueStatus(index - 1);
                    if (result.status == false) {
                        $.zero.showWarnMsg(result.msg);
                        return;
                    }

                    if (index >= 3 && index <= 5) {
                        this.submitUpdateQuote(index);
                    }

                    this.selectedProcessIndex = index;

                    $("html,body").scrollTop(10); //进度条修改状态
                },
                loadProcess: function (index) {
                    if (this.selectedProcessIndex == index) return;
                    index = index > this.stepProcess.length ? this.stepProcess.length : index;

                    if (this.selectedProcessIndex < index) {
                        for (var i = 2; i <= index; i++) {
                            var result = this.checkProcessContinueStatus(i - 1);
                            if (result.status == false) {
                                this.selectedProcessIndex = i - 1;
                                return;
                            }
                        }
                    }
                    this.selectedProcessIndex = index;
                    $("html,body").scrollTop(10); //进度条修改状态
                },
                selectedProcess: function (index) {
                    if (this.selectedProcessIndex == index) return;
                    index = index > this.stepProcess.length ? this.stepProcess.length : index;

                    if (this.selectedProcessIndex < index) {
                        for (var i = 2; i <= index; i++) {
                            var result = this.checkProcessContinueStatus(i - 1);
                            if (result.status == false) {
                                $.zero.showWarnMsg('The {0} step is not perfect.<br/>{1}'.replace('{0}', i - 1).replace('{1}', result.msg));
                                return;
                            }
                        }
                    }
                    this.selectedProcessIndex = index;
                    $("html,body").scrollTop(10); //进度条修改状态
                },
                checkProcessContinueStatus: function (index) {
                    var result = { status: true, msg: '' };
                    switch (index) {
                        case 1:
                            if (this.orderItem.productId <= 0) {
                                result.status = false;
                                result.msg = 'Please choose the product.';
                            }
                            break;
                        case 2:
                            if (this.orderItem.size <= 0) {
                                result.status = false;
                                result.msg = 'Please choose the size.';
                                break;
                            }

                            if (this.orderItem.embroideryCoverageRequired == true && this.orderItem.embroideryCoverageId <= 0) {
                                result.status = false;
                                result.msg = 'Please choose embroidery coverage';
                                break;
                            }

                            if (this.orderItem.typeRequired == true && this.orderItem.typeId <= 0) {
                                result.status = false;
                                result.msg = 'Please choose type';
                                break;
                            }

                            if (this.orderItem.quantity <= 0) {
                                result.status = false;
                                result.msg = 'Please choose the quantity.';
                            }
                            break;
                        case 3:
                            break;
                        case 4:
                            //if (this.orderItem.fileList.length == 0 && this.orderItem.isUploadLater == false) {
                            //    result.status = false;
                            //    result.msg = 'Please upload pictures, or choose to upload later.';
                            //}
                            break;
                        case 5:
                            break;
                        default:
                            result.status = false;
                            result.msg = 'Unknown steps';
                            break;
                    }
                    return result;
                },
                getPhoto(photoData, index, width, height) {
                    return $.zero.getPhoto(photoData, index, width, height);
                },
                changeSize: function () {
                    if (this.orderItem.selectedSize === 'Custom Size') {
                        var re = /^[0-9]*([.][0-9]{1,2})?$/;
                        var width = this.orderItem.width;
                        if (re.test(width) === false || parseFloat(width) < vm.orderItem.minSize || parseFloat(width) > vm.orderItem.maxSize) {
                            $('input[name="width"]').addClass('red_tip');
                            width = 0;
                        }
                        else {
                            $('input[name="width"]').removeClass('red_tip');
                            width = parseFloat(width);
                        }
                        var height = this.orderItem.height;
                        if (re.test(height) === false || parseFloat(height) < vm.orderItem.minSize || parseFloat(height) > vm.orderItem.maxSize) {
                            $('input[name="height"]').addClass('red_tip');
                            height = 0;
                        }
                        else {
                            $('input[name="height"]').removeClass('red_tip');
                            height = parseFloat(height);
                        }

                        if (width > 0 && height > 0) {
                            var size = ((width + height) / 2).toDecimal();
                            this.orderItem.size = size.toString();
                            $('#customSize_tip').removeClass('hide');

                            if (this.quote === null || this.quote.width !== this.orderItem.width) {
                                $.zero.setCurrentQueryString('w', this.orderItem.width);
                            }

                            if (this.quote === null || this.quote.height !== this.orderItem.height) {
                                $.zero.setCurrentQueryString('h', this.orderItem.height);
                            }
                            $.zero.removeCurrentQueryString('s');
                        }
                        else {
                            this.orderItem.size = '';
                            $('#customSize_tip').addClass('hide');
                        }
                    }
                    else {
                        this.orderItem.size = this.orderItem.selectedSize;
                        $('input[name="width"]').removeClass('red_tip');
                        $('input[name="height"]').removeClass('red_tip');
                        $('#customSize_tip').addClass('hide');
                        if (this.quote === null || this.quote.size !== this.orderItem.size) {
                            $.zero.setCurrentQueryString('s', this.orderItem.size);
                            $.zero.removeCurrentQueryString('w');
                            $.zero.removeCurrentQueryString('h');
                        }
                    }
                    this.setProcessContinueStatus(2);
                    this.computePrice();
                },
                changeQuantity: function () {
                    if (this.orderItem.selectedQuantity === 'Custom Quantity') {
                        var re = /^[0-9]*[1-9][0-9]*$/;
                        var value = this.orderItem.customQuantity;
                        if (re.test(value) === false || parseInt(value) < vm.orderItem.minQuantity || parseInt(value) > vm.orderItem.maxQuantity) {
                            $('input[name="customQuantity"]').addClass('red_tip');
                            $('#customQuantity_tip').removeClass('hide');
                            this.orderItem.quantity = 0;
                        }
                        else {
                            $('input[name="customQuantity"]').removeClass('red_tip');
                            $('#customQuantity_tip').addClass('hide');
                            this.orderItem.quantity = value;
                        }
                    }
                    else {
                        $('input[name="customQuantity"]').removeClass('red_tip');
                        $('#customQuantity_tip').addClass('hide');
                        this.orderItem.quantity = this.orderItem.selectedQuantity;
                    }
                    if ((this.quote === null || this.quote.quantity !== this.orderItem.quantity) && this.orderItem.quantity > 0) {
                        $.zero.setCurrentQueryString('q', this.orderItem.quantity);
                    }

                    this.setProcessContinueStatus(2);
                    this.computePrice();
                },
                findSizePriceSection: function (source, size) {
                    //筛选出区间内同尺寸得不同数量列表
                    var sizePriceList = [];
                    if (size <= source[0].size) {
                        //0--最低区间内
                        sizePriceList = $.zero.where(source, function () {
                            return this.size === source[0].size;
                        });
                    }
                    else if (size >= source[source.length - 1].size) {
                        //最高区间内--n
                        sizePriceList = $.zero.where(source, function () {
                            return this.size === source[source.length - 1].size;
                        });
                    }
                    else {
                        //最低区间--最高区间
                        sizePriceList = $.zero.where(source, function () {
                            return this.size >= size;
                        });

                        sizePriceList = $.zero.where(sizePriceList, function () {
                            return this.size === sizePriceList[0].size;
                        });
                    }
                    return sizePriceList;
                },
                findQuantityPriceSection: function (source, quantity) {
                    //筛选出尺寸区间内的数量
                    var quantityPriceList = [];
                    if (quantity <= source[0].quantity) {
                        //0--最低区间内
                        quantityPriceList = $.zero.where(source, function () {
                            return this.quantity === source[0].quantity;
                        });
                    }
                    else if (quantity >= source[source.length - 1].quantity) {
                        //最高区间内-n
                        quantityPriceList = $.zero.where(source, function () {
                            return this.quantity === source[source.length - 1].quantity;
                        });
                    }
                    else {
                        for (var i = 0; i < source.length; i++) {
                            //刚好等于区间数量
                            if (source[i].quantity === quantity) {
                                quantityPriceList.push(source[i]);
                                return quantityPriceList;
                            }
                            //刚好介于区间数量内
                            if (source[i].quantity < quantity && quantity < source[i + 1].quantity) {
                                quantityPriceList.push(source[i]);
                                quantityPriceList.push(source[i + 1]);
                                return quantityPriceList;
                            }
                        }
                    }
                    return quantityPriceList;
                },
                computePrice: function () {
                    //选择产品，有价格配置
                    var productItem = $.zero.find(this.productNode, 'id', this.orderItem.productId);
                    if (productItem === null || productItem.priceBase === undefined || productItem.priceDiscount === undefined) return;

                    this.orderItem.unitPrice = 0;
                    this.orderItem.discount = 0;
                    this.orderItem.originalAmount = 0;
                    this.orderItem.discountFee = 0;
                    this.orderItem.discountAmount = 0;
                    this.orderItem.discountPrice = 0;

                    if (this.orderItem.size > 0 && (this.orderItem.embroideryCoverageRequired === false || this.orderItem.embroideryCoverageId > 0)) {

                        var size = parseFloat(this.orderItem.size);
                        var sizePriceBase = [];
                        var sizePriceDiscount = [];
                        var quantityPriceBase = [];
                        var quantityPriceDiscount = [];

                        if (this.orderItem.embroideryCoverageId != 0) {
                            sizePriceBase = $.zero.where(productItem.priceBase, function () {
                                return this.type == 'b-' + vm.orderItem.embroideryCoverageId;
                            });

                            sizePriceDiscount = $.zero.where(productItem.priceDiscount, function () {
                                return this.type == 'd-' + vm.orderItem.embroideryCoverageId;
                            });
                        }
                        else {
                            sizePriceBase = productItem.priceBase;
                            sizePriceDiscount = productItem.priceDiscount;
                        }

                        if (sizePriceBase.length > 0) {
                            sizePriceBase = this.findSizePriceSection(sizePriceBase, size);
                        }

                        if (sizePriceDiscount.length > 0) {
                            sizePriceDiscount = this.findSizePriceSection(sizePriceDiscount, size);
                        }
                        var quantity = 0;
                        //推荐数量价格
                        if (this.quantityNode != null && this.quantityNode.children != null) {
                            for (var i = 0; i < this.quantityNode.children.length; i++) {
                                var quantityItem = this.quantityNode.children[i];

                                quantity = parseFloat(quantityItem.name);
                                quantityPriceBase = this.findQuantityPriceSection(sizePriceBase, quantity);
                                quantityPriceDiscount = this.findQuantityPriceSection(sizePriceDiscount, quantity);

                                var unitPrice = this.getBasePriceAndFee(quantityPriceBase, quantity);
                                var discount = parseFloat(quantityPriceDiscount[quantityPriceDiscount.length - 1].content);
                                var originalAmount = (quantity * unitPrice).toDecimal();
                                var discountFee = (originalAmount * discount / 100).toDecimal();
                                var discountAmount = (originalAmount - discountFee).toDecimal();
                                var discountPrice = (discountAmount / quantity).toDecimal();
                                Vue.set(quantityItem, 'unitPrice', unitPrice);
                                Vue.set(quantityItem, 'originalAmount', originalAmount);
                                Vue.set(quantityItem, 'discountPrice', discountPrice);
                                Vue.set(quantityItem, 'discountAmount', discountAmount);
                            }
                        }

                        if (this.orderItem.quantity > 0) {
                            quantity = parseFloat(this.orderItem.quantity);
                            quantityPriceBase = this.findQuantityPriceSection(sizePriceBase, quantity);
                            quantityPriceDiscount = this.findQuantityPriceSection(sizePriceDiscount, quantity);

                            this.orderItem.unitPrice = this.getBasePriceAndFee(quantityPriceBase, quantity);
                            this.orderItem.discount = parseFloat(quantityPriceDiscount[quantityPriceDiscount.length - 1].content);
                            this.orderItem.originalAmount = (quantity * this.orderItem.unitPrice).toDecimal();
                            this.orderItem.discountFee = (this.orderItem.originalAmount * this.orderItem.discount / 100).toDecimal();
                            this.orderItem.discountAmount = (this.orderItem.originalAmount - this.orderItem.discountFee).toDecimal();
                            this.orderItem.discountPrice = (this.orderItem.discountAmount / quantity).toDecimal();
                        }
                    }
                    this.computeOtherPrice();
                    this.computeFee();
                },
                getBasePriceAndFee(quantityPriceList, quantity) {
                    //todo:需要辨别新值和旧值是否不一致 
                    var price = parseFloat(quantityPriceList[quantityPriceList.length - 1].content);
                    if (quantityPriceList.length > 1) {
                        var startQuantity = parseFloat(quantityPriceList[quantityPriceList.length - 2].quantity);
                        var startPrice = parseFloat(quantityPriceList[quantityPriceList.length - 2].content);
                        var endQuantity = parseFloat(quantityPriceList[quantityPriceList.length - 1].quantity);
                        var endPrice = parseFloat(quantityPriceList[quantityPriceList.length - 1].content);

                        var differenceFee = endQuantity * endPrice - startQuantity * startPrice;
                        if (differenceFee <= 0 || endPrice === startPrice) return price;
                        var difference = differenceFee / (endQuantity - startQuantity);
                        var fee = startQuantity * startPrice + (quantity - startQuantity) * difference;
                        price = (fee / quantity).toDecimal();
                    }
                    return price;
                },
                computeOtherPrice: function () {
                    if (this.orderItem.quantity > 0) {
                        //查找数量区间编号
                        var quantityEnd = $.zero.find(_quantityConfig, function () {
                            return this.min <= vm.orderItem.quantity && vm.orderItem.quantity <= this.max;
                        });

                        //在区间中央
                        var quantityStart = null;
                        if (quantityEnd !== null && this.orderItem.quantity !== quantityEnd.max) {
                            var quantityStartList = $.zero.where(_quantityConfig, function () {
                                return this.max < vm.orderItem.quantity;
                            });
                            quantityStart = quantityStartList.length === 0 ? null : quantityStartList[quantityStartList.length - 1];
                        }

                        //不在范围内
                        if (quantityEnd === null) {
                            //在第一个之前
                            if (this.orderItem.quantity < _quantityConfig[0].max) {
                                quantityEnd = _quantityConfig[0];
                            }
                            //在最后一个之后
                            if (this.orderItem.quantity > _quantityConfig[_quantityConfig.length - 1].max) {
                                quantityEnd = _quantityConfig[_quantityConfig.length - 1];
                            }
                        }
                        var result = null;

                        //特殊选项费用
                        if (this.specialOptionNode != null && this.specialOptionNode.children != null) {
                            for (var i = 0; i < this.specialOptionNode.children.length; i++) {
                                var specialOptionParent = this.specialOptionNode.children[i];
                                if (specialOptionParent.children != null) {
                                    for (var j = 0; j < specialOptionParent.children.length; j++) {
                                        specialOptionChildren = specialOptionParent.children[j];
                                        result = this.getOtherPriceAndFee(quantityStart, quantityEnd, specialOptionChildren.configData);
                                        Vue.set(specialOptionChildren, 'fee', result.fee);
                                    }
                                }
                            }
                        }
                        //物流选项
                        if (this.turnaroundTypeNode != null && this.turnaroundTypeNode.children != null) {
                            for (var i = 0; i < this.turnaroundTypeNode.children.length; i++) {
                                var turnaroundTypeItem = this.turnaroundTypeNode.children[i];
                                //result = this.getOtherPriceAndFee(quantityStart, quantityEnd, turnaroundTypeItem.configData);
                                result = this.getTurnaroundPriceAndFee(quantityStart, quantityEnd, turnaroundTypeItem.configData);
                                Vue.set(turnaroundTypeItem, 'fee', result.fee);
                            }
                        }
                    }
                },
                getOtherPriceAndFee(quantityStart, quantityEnd, configData) {
                    var result = { price: 0, fee: 0 };
                    if (configData !== null && configData !== '') {
                        var priceData = JSON.parse(configData);
                        priceEnd = $.zero.find(priceData, 'id', quantityEnd.id);
                        if (priceEnd !== null && priceEnd.price > 0) {
                            if (quantityStart !== null) {
                                //阶梯计算
                                var priceStart = $.zero.find(priceData, 'id', quantityStart.id);

                                if (priceStart !== null && priceStart.price > 0) {
                                    //todo:difference必须大于0
                                    var differenceFee = quantityEnd.max * priceEnd.price - quantityStart.max * priceStart.price;
                                    if (differenceFee <= 0 || priceEnd.price === priceStart.price) {
                                        result.price = priceEnd.price;
                                        result.fee = (this.orderItem.quantity * result.price).toDecimal();
                                        return result;
                                    }
                                    var difference = differenceFee / (quantityEnd.max - quantityStart.max);
                                    result.fee = quantityStart.max * priceStart.price + (this.orderItem.quantity - quantityStart.max) * difference;
                                    result.price = (result.fee / this.orderItem.quantity).toDecimal();
                                    result.fee = (result.price * this.orderItem.quantity).toDecimal();//根据最终四舍五入的价格来
                                }
                            }
                            else {
                                result.price = priceEnd.price;
                                result.fee = (this.orderItem.quantity * result.price).toDecimal();
                            }
                        }
                    }
                    return result;
                },
                getTurnaroundPriceAndFee(quantityStart, quantityEnd, configData) {
                    var result = { price: 0, fee: 0 };
                    if (configData !== null && configData !== '') {
                        var priceData = JSON.parse(configData);
                        priceEnd = $.zero.find(priceData, 'id', quantityEnd.id);
                        if (priceEnd !== null && priceEnd.price > 0) {
                            result.price = priceEnd.price;
                            result.fee = priceEnd.fee;
                        }
                    }
                    return result;
                },
                useCouponDiscount: function (isUseCoupon) {
                    this.orderItem.isUseCoupon = false;
                    if (isUseCoupon) {
                        if (this.orderItem.couponCode.length <= 0) {
                            $.zero.showErrorMsg('Please enter the discount code');
                            return;
                        }

                        $.ajax({
                            type: "get",
                            url: "/api/coupon.json",
                            data: { code: this.orderItem.couponCode },
                            async: false,
                            success: function (data) {
                                if (data === null) {
                                    $.zero.showErrorMsg('No corresponding discount code found');
                                    return;
                                }

                                if (data.status !== 0) {
                                    $.zero.showErrorMsg('The discount code has expired');
                                    return;
                                }
                                vm.orderItem.isUseCoupon = true;
                                vm.orderItem.couponDiscount = data.discount;
                                vm.computeFee();
                            }
                        });
                    }
                    else {
                        this.orderItem.couponDiscount = 0;
                        this.computeFee();
                    }
                },
                computeFee: function () {
                    this.orderItem.specialOptionFee = 0;
                    for (var i = 0; i < this.orderItem.specialOptionList.length; i++) {
                        specialOptionSelected = this.orderItem.specialOptionList[i];
                        var specialOptionParent = $.zero.find(this.specialOptionNode.children, 'id', specialOptionSelected.pid);
                        specialOptionChild = $.zero.find(specialOptionParent.children, 'id', specialOptionSelected.id);

                        Vue.set(specialOptionSelected, 'fee', specialOptionChild.fee);
                        this.orderItem.specialOptionFee += specialOptionSelected.fee;
                    }

                    if (this.orderItem.turnaroundType != '' && this.turnaroundTypeNode != null && this.turnaroundTypeNode.children != null) {
                        var turnaroundType = $.zero.find(this.turnaroundTypeNode.children, 'name', this.orderItem.turnaroundType);
                        if (turnaroundType != null) {
                            this.orderItem.expediteFee = turnaroundType.fee;
                        }
                    }

                    this.orderItem.subTotal = (this.orderItem.originalAmount + this.orderItem.specialOptionFee).toDecimal();
                    //this.orderItem.additional = ((this.orderItem.subTotal + this.orderItem.expediteFee) * this.orderItem.discount / 100).toDecimal();
                    this.orderItem.additional = (this.orderItem.subTotal * this.orderItem.discount / 100).toDecimal(); //默认优惠折扣金额
                    this.orderItem.couponAmoun = (this.orderItem.subTotal * this.orderItem.couponDiscount / 100).toDecimal(); //优惠券折扣金额
                    this.orderItem.finalTotal = (this.orderItem.subTotal + this.orderItem.expediteFee - this.orderItem.additional - this.orderItem.couponAmoun).toDecimal();
                },
                checkSpecialOptionSelectedEnable: function (specialOptionItem) {
                    //判断是否支持该选项
                    if (specialOptionItem.supportStyleIds != null && specialOptionItem.supportStyleIds != '') {
                        var supportStyleIds = specialOptionItem.supportStyleIds.split(',');

                        if (supportStyleIds.indexOf(this.orderItem.typeId.toString()) === -1) {
                            return false;
                        }
                    }
                    return true;
                },
                checkSpecialOptionSelectedStatus: function (specialOption, specialOptionItem) {
                    if (this.checkSpecialOptionSelectedEnable(specialOptionItem) === false) return 'disable';

                    var selectedSpecialOptionItem = $.zero.find(this.orderItem.specialOptionList, { 'cateId': specialOption.id, 'itemId': specialOptionItem.id });
                    if (selectedSpecialOptionItem != null) {
                        return 'cur';
                    }
                    return '';
                },
                changeSpecialOptionSelectedStatus: function (specialOption, specialOptionItem, isLoad) {
                    //判断是否支持该选项
                    if (this.checkSpecialOptionSelectedEnable(specialOptionItem) === false) return;

                    var selectedSpecialOptionItem = null;
                    if (specialOption.isMultiple == 1) {
                        //多选
                        selectedSpecialOptionItem = $.zero.find(this.orderItem.specialOptionList, { 'cateId': specialOption.id, 'itemId': specialOptionItem.id });

                        if (selectedSpecialOptionItem == null) {
                            this.orderItem.specialOptionList.push(
                                $.extend(true, {
                                    cateId: specialOption.id,
                                    cateName: specialOption.name,
                                    itemId: specialOptionItem.id,
                                    itemName: specialOptionItem.name
                                }, specialOptionItem)
                            );
                        }
                        else {
                            //移除
                            $.zero.remove(this.orderItem.specialOptionList, { 'cateId': specialOption.id, 'itemId': specialOptionItem.id });
                        }
                    }
                    else {
                        //单选
                        selectedSpecialOptionItem = $.zero.find(this.orderItem.specialOptionList, { 'cateId': specialOption.id });

                        if (selectedSpecialOptionItem == null) {
                            this.orderItem.specialOptionList.push(
                                $.extend(true, {
                                    cateId: specialOption.id,
                                    cateName: specialOption.name,
                                    itemId: specialOptionItem.id,
                                    itemName: specialOptionItem.name
                                }, specialOptionItem)
                            );
                        }
                        else {
                            if (selectedSpecialOptionItem.itemId != specialOptionItem.id) {
                                //替换
                                $.extend(true, selectedSpecialOptionItem, specialOptionItem, {
                                    'itemId': specialOptionItem.id,
                                    'itemName': specialOptionItem.name
                                });
                            }
                            else {
                                //移除
                                $.zero.remove(this.orderItem.specialOptionList, { 'cateId': specialOption.id, 'itemId': specialOptionItem.id });
                            }
                        }
                    }
                    if (isLoad === false) {
                        var specialOptionIdList = [];
                        for (var i = 0; i < this.orderItem.specialOptionList.length; i++) {
                            var id = this.orderItem.specialOptionList[i].id;
                            if (specialOptionIdList.indexOf(id) === -1) {
                                specialOptionIdList.push(id);
                            }
                        }
                        this.orderItem.specialOptionIds = specialOptionIdList.length > 0 ? specialOptionIdList.join(',') : '';
                        $.zero.setCurrentQueryString('so', this.orderItem.specialOptionIds);

                        this.computeFee();
                    }
                },
                changeTurnaroundTypeSelectedStatus: function (turnaroundTypeItem, isLoad) {
                    this.orderItem.turnaroundTypeId = turnaroundTypeItem.id;
                    this.orderItem.turnaroundType = turnaroundTypeItem.name;
                    this.orderItem.turnaroundDateRange = this.getTurnaroundDateRange(turnaroundTypeItem);

                    if (isLoad === false) {
                        if (this.orderItem.turnaroundTypeId > 0 && (this.quote === null || this.quote.turnaroundTypeId !== this.orderItem.turnaroundTypeId)) {
                            $.zero.setCurrentQueryString('ttt', this.orderItem.turnaroundTypeId);
                        }
                        var date = $("#datetime" + turnaroundTypeItem.id).val();
                        this.orderItem.turnaroundDate = date != undefined ? date : '';

                        this.computeFee();
                    }
                },
                getTurnaroundDateRange: function (turnaroundType) {
                    var currentDate = new Date();
                    var minDate = $.zero.addDate(currentDate, parseInt(turnaroundType.title));
                    var maxDate = $.zero.addDate(currentDate, parseInt(turnaroundType.subTitle));
                    minDate = new Date(minDate).toDateString();
                    maxDate = new Date(maxDate).toDateString();
                    var minDateList = minDate.split(' ');
                    var maxDateList = maxDate.split(' ');
                    return minDateList[1] + ' ' + minDateList[2] + ' - ' + maxDateList[1] + ' ' + maxDateList[2];
                },
                checkOrder: function () {
                    this.orderItem.fileData = JSON.stringify(this.orderItem.fileList);
                    this.orderItem.specialOptionData = JSON.stringify(this.orderItem.specialOptionList);
                    return true;
                },
                submitOrder: function (data) {
                    console.log(this.orderItem);
                    if (this.checkOrder() === false) return;

                    if (_canSubmitOrder === false) return;
                    _canSubmitOrder = false;
                    $('#submitOrder').html('Submit Order...');
                    $.ajax({
                        type: "post",
                        url: "/api/addOrder",
                        data: this.orderItem,
                        success: function (msg) {
                            if (msg.success) {
                                console.log(msg);
                                gtag_report_conversion_order();
                                location.href = '/success.html';
                            }
                            else {
                                $.zero.showValidationErrorMsg(msg);
                            }
                            _canSubmitOrder = true;
                            $('#submitOrder').html('Submit');
                        },
                        error: function () {
                            // $.zero.showErrorMsg('Sorry Server Lost');
                            _canSubmitOrder = true;
                            $('#submitOrder').html('Submit');
                        }
                    });
                }
            }
        });
    }); //use layui初始化结束
}); //jquery